<?php
global $_W, $_GPC;
load()->func('tpl');
$html_title = $_W['current_module']['title'];
$op = !empty($_GPC['op']) ? $_GPC['op'] : '';
$extra = !empty($_GPC['extra']) ? $_GPC['extra'] : '';
$isactive = 'member';//底部菜单切换效果
$page = max(1, $_GPC['page']);
$start = ($page - 1) * $pagesize;

//个人中心
if ($op == 'index') {
    $html_title = '个人中心';
    $isLogin = 0;
    if (checkMobileLogin()) {
        $isLogin = 1;
        $userinfo = pdo_get('yq_haircircle_users', array('id' => session('user_id')));
    }
    include $this->template('member_index');
    exit;
}
//主屏我的任务
if ($op == 'task') {
    if (is_ajax()) {
        $pagesize = 20;
        $start = ($page - 1) * $pagesize;
        $type = $_GPC['type'] ? intval($_GPC['type']) : 1;//默认是已抢任务
        $user_id = session('user_id');
        if (!$user_id) {
            ajaxReturn(['code' => USER_NOT_LOGIN, 'msg' => '您尚未登录']);
        }
        $field = "A.id receive_id,A.taskid,B.title,B.thumb,B.remark";
        $limit = "$start,$pagesize";
        $where = '';
        switch ($type) {
            case 1:
                break;//已抢任务。即所有任务
            case 2:
                $where .= " and A.receive_status=0 ";
                break;//未提交任务
            case 3:
                $where .= " and A.receive_status in (2,3) ";
                break;//已提交任务
        }
        $sql = "select $field from ims_yq_haircircle_task_receive A left join ims_yq_haircircle_task B on A.taskid=B.id where A.user_id=$user_id $where order by A.id desc limit $limit";
        $list = pdo_fetchall($sql);
        if (empty($list)) {
            $list = array();
            ajaxSuccess('success', $list);
        }
        foreach ($list as $k => $v) {
            $list[$k]['jumpurl'] = $this->createMobileUrl('task', array('op' => 'detail', 'id' => $v['taskid']));
            $list[$k]['thumb'] = cmf_get_upload_url($v['thumb']);
            $list[$k]['remark'] = cutstr_html(htmlspecialchars_decode($v['remark']));
        }
        ajaxSuccess('success', $list);
    }
    checkMobileLogin(true);
    $isactive = 'task';//底部菜单切换效果
    $html_title = '我的任务';
    include $this->template('member_task');
    exit;
}
//个人中心任务详情
if ($op == 'task_list') {
    if (is_ajax()) {
        $pagesize = 20;
        $start = ($page - 1) * $pagesize;
        $type = $_GPC['type'] ? intval($_GPC['type']) : 1;//默认是进行中
        $user_id = session('user_id');
        if (!$user_id) {
            ajaxReturn(['code' => USER_NOT_LOGIN, 'msg' => '您尚未登录']);
        }
        $field = "A.id receive_id,A.taskid,A.receive_time,B.title,B.thumb,B.remark,B.task_money,B.end_time,B.validate";
        $limit = "$start,$pagesize";
        $where = '';
        switch ($type) {
            case 1:
                $where .= " and A.receive_status=0 and A.review_status=0";
                break;//进行中
            case 2:
                $where .= " and A.receive_status=2 and A.review_status=0";
                break;//待审核
            case 3:
                $where .= " and A.review_status=2 and A.receive_status=3";
                break;//被驳回
            case 4:
                $where .= " and A.review_status=1 and A.receive_status=3";
                break;//已通过
        }
        $sql = "select $field from ims_yq_haircircle_task_receive A left join ims_yq_haircircle_task B on A.taskid=B.id where A.user_id=$user_id $where order by A.id desc limit $limit";
        $list = pdo_fetchall($sql);
        if (empty($list)) {
            $list = array();
            ajaxSuccess('success', $list);
        }
        foreach ($list as $k => $v) {
            $list[$k]['jumpurl'] = $this->createMobileUrl('task', array('op' => 'detail', 'id' => $v['taskid']));
            $list[$k]['thumb'] = cmf_get_upload_url($v['thumb']);
            $list[$k]['remark'] = htmlspecialchars_decode($v['remark']);
            $list[$k]['receive_time'] = date('Y-m-d H:i:s', $v['receive_time']);
            $list[$k]['end_time'] = date('Y-m-d H:i:s', $v['end_time']);
        }
        ajaxSuccess('success', $list);
    }
    $html_title = '任务详情';
    checkMobileLogin(true);
    include $this->template('member_task_list');
    exit;
}

//我的团队
if ($op == 'team') {
    $user_id = session('user_id');
    if (!$user_id) {
        is_post() ? ajaxReturn(['code' => USER_NOT_LOGIN, 'msg' => '您尚未登录']) : redirect(url('entry//public', array('m' => 'yq_haircircle', 'op' => 'login')));//跳到登录页面
    }
    $child_child = array();
    $child = pdo_getall('yq_haircircle_users', array('pid' => $user_id), array('id', 'pid', 'mobile', 'user_level'));
    if (empty($child)) {
        $child = array();
    } else {
        foreach ($child as $k => $v) {
            $child[$k]['user_vip'] = $v['user_level'] > 0 ? "会员" : "普通用户";
            $res = pdo_getall('yq_haircircle_users', array('pid' => $v['id']), array('id', 'pid', 'mobile', 'user_level'));
            !empty($res) && array_push($child_child, $res);
        }
    }
    if(!empty($child_child)){
        foreach($child_child as $k => $v){
            $child_child[$k]['user_vip'] = $v['user_level'] > 0 ? "会员" : "普通用户";
        }
    }
    $html_title = '我的团队';
    $teamcount = count($child) + count($child_child);
    $tgyj = pdo_fetchcolumn("select sum(fxyj) as tgyj from ims_yq_haircircle_user_fx_log where user_id={$user_id}");
    $tgyj = $tgyj ? $tgyj : '0.00';
    $user = pdo_get('yq_haircircle_users', array('id' => $user_id));
    include $this->template('member_team');
    exit;
}

//提现列表
if ($op == 'cashout_list') {
    if (is_post()) {
        $pagesize = 20;
        $start = ($page - 1) * $pagesize;
        $user_id = session('user_id');
        if (!$user_id) {
            ajaxReturn(['code' => USER_NOT_LOGIN, 'msg' => '您尚未登录']);
        }
        $field = "cash_money,create_time,cash_status";
        $sql = "select $field from ims_yq_haircircle_cashout where user_id=$user_id order by id desc limit $start,$pagesize";
        $list = pdo_fetchall($sql);
        if (empty($list)) {
            ajaxSuccess('success', $list);
        }
        foreach ($list as $k => $v) {
            $list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            switch ($v['cash_status']) {
                case -1:
                    $list[$k]['tx_state'] = "提现失败";
                    break;
                case 0:
                    $list[$k]['tx_state'] = "提现中";
                    break;
                case 1:
                    $list[$k]['tx_state'] = "提现成功";
                    break;
            }
        }
        ajaxSuccess('success', $list);
    }
    checkMobileLogin(true);
    $html_title = '提现列表';
    include $this->template('member_cashout_list');
    exit;
}

if ($op == 'yongjin') {
    checkMobileLogin(true);
    $html_title = '佣金列表';
    $user_id = session('user_id');
    $sql1 = "select B.taskid,A.fxyj,A.create_time from ims_yq_haircircle_user_fx_log A left join ims_yq_haircircle_task_receive B on A.oid=B.id where A.yong_type=0 and A.user_id=$user_id order by A.id desc";
    $sql2 = "select A.sup_id,A.fxyj,A.create_time from ims_yq_haircircle_user_fx_log A where A.yong_type=1 and A.user_id=$user_id order by A.id desc";
    $list1 = pdo_fetchall($sql1);
    $list2 = pdo_fetchall($sql2);
    if (empty($list1)) {
        $list1 = [];
    } else {
        foreach ($list1 as $k => $v) {
            $list1[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
        }
    }
    if (empty($list2)) {
        $list2 = [];
    } else {
        foreach ($list2 as $k => $v) {
            $list2[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
        }
    }
    include $this->template('member_yongjin');
    exit;
}

//收支列表
if ($op == 'shouzhi') {
    checkMobileLogin(true);
    $html_title = '收支列表';
    include $this->template('member_shouzhi');
    exit;
}